// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var X509 = entd.crypto.OpenSSL.X509;

// X.509 certificate (DER).
const CERT_DATA_DER =
  "308203B53082029DA003020102020900979D8DA71DBE42FF300D06092A864886F70D01" +
  "010505003045310B3009060355040613024155311330110603550408130A536F6D652D" +
  "53746174653121301F060355040A1318496E7465726E65742057696467697473205074" +
  "79204C7464301E170D3130313131393232343732305A170D3130313231393232343732" +
  "305A3045310B3009060355040613024155311330110603550408130A536F6D652D5374" +
  "6174653121301F060355040A1318496E7465726E657420576964676974732050747920" +
  "4C746430820122300D06092A864886F70D01010105000382010F003082010A02820101" +
  "00CB78D8AA4BF290AC26402FF120B22246F63E1A49DB4E05CD2AD580A86E8F4E93C903" +
  "EEBC1E6A17419AEEBCAFDD08C7D9A7202E4A6C6F0AAA7F661E6F607B8FB1880D07EBFB" +
  "E00930F565576A236CBB8DDED6E9B3E4B9679B2613AAC140D237037DF4CBC91D1551DC" +
  "0C1C185A0DC158FBA85D41102C89A6DEFC21B4D4855C12BF6B6563DA8F07A799D9BB3F" +
  "F67BC7978A2FF19D4D0E63DA48B0754DCC3444F5E0EA756A9867237E25868A08DA22FC" +
  "307BF2A8C06D0FA0D4064B1897298ED150B7EFDF82416569A201854ED392EBA6617115" +
  "0C24AD80B1F0F28DEB7C954204D38EAADFE57F0458AE6523ABB2E0F68DCC5FABD55DF8" +
  "C965F69368D24E6FFAE284CD0203010001A381A73081A4301D0603551D0E04160414B6" +
  "77B9FCA5111F646E91A7DF6B31B600D341D91B30750603551D23046E306C8014B677B9" +
  "FCA5111F646E91A7DF6B31B600D341D91BA149A4473045310B30090603550406130241" +
  "55311330110603550408130A536F6D652D53746174653121301F060355040A1318496E" +
  "7465726E6574205769646769747320507479204C7464820900979D8DA71DBE42FF300C" +
  "0603551D13040530030101FF300D06092A864886F70D01010505000382010100A4B0DD" +
  "DB579C0AA68A0ABAF6A32D537389C0A25FF85D11D22C84A723B348856081E5A6E99218" +
  "AEDEB2BAC9704F906B436EBBA2E2B521EDCC42083C69931E1EF792CFBEA99086ED5B54" +
  "A38B121377A31439075FE61E21B1CB36BE25C31D9B23DE59993CEC369F304A596DF218" +
  "AB9E55A0476EF1B98D01D787B445AABF0B816C2897C08AA733619B70BCDCA7AEC2EA1B" +
  "85C56A968AF24230DF70D3EE2CD93C150169E5A7649B7EE64DC6FFE952CE7613A281AF" +
  "9987A3C3A3761D6CBA618AC1744FB9B9B7A59492C8C47917BE68945537B4151DCD73B5" +
  "C1241B2CEFEDFFBFE88F60B2C60AA0BCABE27DFF97FE738306DA94A2C3F5FD4DD591ED" +
  "28386FAC26D5EF2D";

// X.509 certificate (PEM text).
const CERT_DATA_PEM_TEXT =
  "-----BEGIN CERTIFICATE-----\n" +
  "MIIDtTCCAp2gAwIBAgIJAJedjacdvkL/MA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV\n" +
  "BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX\n" +
  "aWRnaXRzIFB0eSBMdGQwHhcNMTAxMTE5MjI0NzIwWhcNMTAxMjE5MjI0NzIwWjBF\n" +
  "MQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50\n" +
  "ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\n" +
  "CgKCAQEAy3jYqkvykKwmQC/xILIiRvY+GknbTgXNKtWAqG6PTpPJA+68HmoXQZru\n" +
  "vK/dCMfZpyAuSmxvCqp/Zh5vYHuPsYgNB+v74Akw9WVXaiNsu43e1umz5LlnmyYT\n" +
  "qsFA0jcDffTLyR0VUdwMHBhaDcFY+6hdQRAsiabe/CG01IVcEr9rZWPajwenmdm7\n" +
  "P/Z7x5eKL/GdTQ5j2kiwdU3MNET14Op1aphnI34lhooI2iL8MHvyqMBtD6DUBksY\n" +
  "lymO0VC379+CQWVpogGFTtOS66ZhcRUMJK2AsfDyjet8lUIE046q3+V/BFiuZSOr\n" +
  "suD2jcxfq9Vd+Mll9pNo0k5v+uKEzQIDAQABo4GnMIGkMB0GA1UdDgQWBBS2d7n8\n" +
  "pREfZG6Rp99rMbYA00HZGzB1BgNVHSMEbjBsgBS2d7n8pREfZG6Rp99rMbYA00HZ\n" +
  "G6FJpEcwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNV\n" +
  "BAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZIIJAJedjacdvkL/MAwGA1UdEwQF\n" +
  "MAMBAf8wDQYJKoZIhvcNAQEFBQADggEBAKSw3dtXnAqmigq69qMtU3OJwKJf+F0R\n" +
  "0iyEpyOzSIVggeWm6ZIYrt6yuslwT5BrQ267ouK1Ie3MQgg8aZMeHveSz76pkIbt\n" +
  "W1SjixITd6MUOQdf5h4hscs2viXDHZsj3lmZPOw2nzBKWW3yGKueVaBHbvG5jQHX\n" +
  "h7RFqr8LgWwol8CKpzNhm3C83KeuwuobhcVqloryQjDfcNPuLNk8FQFp5adkm37m\n" +
  "Tcb/6VLOdhOiga+Zh6PDo3YdbLphisF0T7m5t6WUksjEeRe+aJRVN7QVHc1ztcEk\n" +
  "Gyzv7f+/6I9gssYKoLyr4n3/l/5zgwbalKLD9f1N1ZHtKDhvrCbV7y0=\n" +
  "-----END CERTIFICATE-----";

// X.509 certificate (PEM binary).
const CERT_DATA_PEM =
  "2D2D2D2D2D424547494E2043455254494649434154452D2D2D2D2D0A4D494944745443" +
  "4341703267417749424167494A414A65646A616364766B4C2F4D413047435371475349" +
  "623344514542425155414D455578437A414A42674E560A42415954416B46564D524D77" +
  "4551594456515149457770546232316C4C564E305958526C4D53457748775944565151" +
  "4B4578684A626E526C636D356C644342580A6157526E6158527A494642306553424D64" +
  "4751774868634E4D5441784D5445354D6A49304E7A49775768634E4D5441784D6A4535" +
  "4D6A49304E7A4977576A42460A4D517377435159445651514745774A42565445544D42" +
  "45474131554543424D4B553239745A533154644746305A5445684D4238474131554543" +
  "684D59535735300A5A584A755A58516756326C6B5A326C306379425164486B67544852" +
  "6B4D494942496A414E42676B71686B6947397730424151454641414F43415138414D49" +
  "49420A43674B434151454179336A59716B76796B4B776D51432F78494C49695276592B" +
  "476B6E625467584E4B745741714736505470504A412B3638486D6F58515A72750A764B" +
  "2F64434D665A70794175536D78764371702F5A683576594875507359674E422B763734" +
  "416B773957565861694E737534336531756D7A354C6C6E6D7959540A71734641306A63" +
  "446666544C795230565564774D48426861446346592B36686451524173696162652F43" +
  "473031495663457239725A5750616A77656E6D646D370A502F5A377835654B4C2F4764" +
  "5451356A326B69776455334D4E455431344F70316170686E4933346C686F6F4932694C" +
  "384D487679714D427444364455426B73590A6C796D4F3056433337392B43515756706F" +
  "67474654744F5336365A686352554D4A4B3241736644796A6574386C55494530343671" +
  "332B562F424669755A534F720A737544326A637866713956642B4D6C6C39704E6F306B" +
  "35762B754B457A514944415141426F34476E4D49476B4D423047413155644467515742" +
  "42533264376E380A705245665A473652703939724D6259413030485A477A423142674E" +
  "5648534D45626A42736742533264376E38705245665A473652703939724D6259413030" +
  "485A0A4736464A704563775254454C4D416B474131554542684D4351565578457A4152" +
  "42674E5642416754436C4E766257557455335268644755784954416642674E560A4241" +
  "6F5447456C7564475679626D5630494664705A47647064484D6755485235494578305A" +
  "49494A414A65646A616364766B4C2F4D41774741315564457751460A4D414D42416638" +
  "774451594A4B6F5A496876634E415145464251414467674542414B5377336474586E41" +
  "716D6967713639714D7455334F4A774B4A662B4630520A3069794570794F7A53495667" +
  "6765576D365A49597274367975736C7754354272513236376F754B314965334D516767" +
  "38615A4D65487665537A3736706B4962740A5731536A6978495464364D554F51646635" +
  "6834687363733276695844485A736A336C6D5A504F77326E7A424B57573379474B7565" +
  "56614248627647356A5148580A683752467172384C6757776F6C38434B707A4E686D33" +
  "4338334B657577756F62686356716C6F7279516A4466634E50754C4E6B384651467035" +
  "61646B6D33376D0A5463622F36564C4F64684F6967612B5A683650446F335964624C70" +
  "686973463054376D35743657556B736A456552652B614A52564E3751564863317A7463" +
  "456B0A47797A7637662B2F364939677373594B6F4C7972346E332F6C2F357A67776261" +
  "6C4B4C443966314E315A48744B446876724362563779303D0A2D2D2D2D2D454E442043" +
  "455254494649434154452D2D2D2D2D0A";

function testCertExport(x509, source, source_type) {
  var x509_export;

  // PEM tests.
  x509_export = x509.toFormat(X509.X509_FORMAT_PEM);
  if (!x509_export)
    return println("Failed to export certificate in PEM format.");
  if (source_type == X509.X509_FORMAT_PEM)
    if (x509_export.toUpperCase() != source.toUpperCase())
      return println("Exported PEM data does not match original input.");

  // PEM text tests.
  x509_export = x509.toFormat(X509.X509_FORMAT_PEM_TEXT);
  if (!x509_export)
    return println("Failed to export certificate in PEM text format.");
  if (source_type == X509.X509_FORMAT_PEM_TEXT)
    if (x509_export.replace(/\n/gi, "") != source.replace(/\n/gi, ""))
      return println("Exported PEM text data does not match original input.");

  // DER tests.
  x509_export = x509.toFormat(X509.X509_FORMAT_DER);
  if (!x509_export)
    return println("Failed to export certificate in DER format.");
  if (source_type == X509.X509_FORMAT_DER)
    if (x509_export.toUpperCase() != source.toUpperCase())
      return println("Exported DER data does not match original input.");

  return true;
}

entd.onLoad = function () {
  var x509;

  // Test certificate in PEM format.
  x509 = new entd.crypto.OpenSSL.X509(CERT_DATA_PEM, X509.X509_FORMAT_PEM);
  if (!(x509 instanceof X509))
    return println("Failed to create X.509 (PEM) certificate object.");
  if (!testCertExport(x509, CERT_DATA_PEM, X509.X509_FORMAT_PEM))
    return;
  if (x509)
    x509.dispose();

  // Test certificate in PEM text format.
  x509 = new entd.crypto.OpenSSL.X509(CERT_DATA_PEM_TEXT,
                                      X509.X509_FORMAT_PEM_TEXT);
  if (!(x509 instanceof X509))
    return println("Failed to create X.509 (PEM) text certificate object.");
  if (!testCertExport(x509, CERT_DATA_PEM_TEXT, X509.X509_FORMAT_PEM_TEXT))
    return;
  if (x509)
    x509.dispose();

  // Test certificate in DER format.
  x509 = new entd.crypto.OpenSSL.X509(CERT_DATA_DER, X509.X509_FORMAT_DER);
  if (!(x509 instanceof X509))
    return println("Failed to create X.509 (DER) certificate object.");
  if (!testCertExport(x509, CERT_DATA_DER, X509.X509_FORMAT_DER))
    return;
  if (x509)
    x509.dispose();

  println("LOOKS OK");
}
